package edu.sctu2;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.*;

public class Login2 extends HttpServlet{
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String username=req.getParameter("username");

        String password=req.getParameter("password");

        try {
            Class.forName("com.mysql.jdbc.Driver");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }

        Connection connection=null;
        Statement statement=null;
        ResultSet resultSet=null;

        try {
            connection= DriverManager.getConnection("jdbc:mysql://localhost:3306/sctu_javaweb_2018?serverTimezone=UTC&characterEncoding=gbk",
                    "root",
                    "w19971201t");
            statement=connection.createStatement();
            resultSet=statement.executeQuery("select * from t_user2");
            while (resultSet.next()){
                if (username.equals(resultSet.getString(2))&&password.equals(resultSet.getString(3))){
                    req.getRequestDispatcher("/edu/sctu2/Userlist").forward(req,resp);
                }
                else {
                    req.getRequestDispatcher("/login.jsp").forward(req,resp);

                }

            }
        } catch (SQLException e) {
            e.printStackTrace();
        }



        finally {
            try {
                connection.close();
                statement.close();
                resultSet.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }

    }
}
